import paddlehub as hub
import pandas as pd
import warnings
import matplotlib.pyplot as plt
import matplotlib
from matplotlib import font_manager
import akshare as ak
warnings.filterwarnings("ignore") 
font_path = 'SimHei.ttf'  # 设置本地字体
font_manager.fontManager.addfont(font_path)
prop = font_manager.FontProperties(fname=font_path)
matplotlib.use('Qt5Agg')
warnings.filterwarnings("ignore") 
def emt(fccode):
	data_raw = pd.read_excel("./file/基金"+fccode+"的前100页评论.xlsx")
	data_raw['time'] = pd.to_datetime('2021 '+data_raw['time'])
	senta = hub.Module(name="senta_bilstm")
	texts = data_raw['title'].tolist()
	input_data = {'text':texts}
	res = senta.sentiment_classify(data=input_data) ##用百度的paddlehub NLP模型来预测情感倾向
	data_raw['情绪'] = [x['positive_probs'] for x in res]
	data_raw.index = data_raw['time']
	data = data_raw.resample('15min').mean().reset_index() ##重采样至15min
	sz_index = ak.stock_zh_a_minute(symbol='sh000001', period='15', adjust="qfq")
	sz_index['day'] = pd.to_datetime(sz_index['day'])
	sz_index['close'] = sz_index['close'].astype('float')
	data = data.merge(sz_index,left_on='time',right_on='day',how='inner')
	data.index = data['time']
	data[['情绪','close']].plot(secondary_y=['close'])
	#fig = plt.figure(figsize=(20,8)) 
	plt.title("基金"+fccode+"的基民情绪与大盘走势关系.png")
	plt.savefig("./file/基金"+fccode+"的基民情绪与大盘走势关系.png")